<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Delivery;
use App\Models\Customer;
use App\Models\Goods;
use App\Models\Manufacturer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class DeliveryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Delivery(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_id','产品名称')->display(function() {
                return Goods::find($this->goods_id)->goods_name;
            });
            $grid->column('manufacturer_name','厂家名称')->display(function() {
                $manufacturer_id = Goods::find($this->goods_id)->manufacturer_id;
                return Manufacturer::find($manufacturer_id)->manufacturer_name;
            });
            $grid->column('delivery_date');
            $grid->column('customer_id','客户名称')->display(function() {
                return Customer::find($this->customer_id)->customer_name;
            });
            $grid->column('num');
            $grid->column('price');
            $grid->column('total_price');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Delivery(), function (Show $show) {
            $show->field('id');
            $show->field('goods_name','产品名称')->display(function() {
                return Goods::find($this->goods_id)->goods_name;
            });
            $show->field('delivery_date');
            $show->field('customer_name','客户名称')->display(function() {
                return Customer::find($this->customer_id)->customer_name;
            });
            $show->field('num');
            $show->field('price');
            $show->field('total_price');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Delivery(), function (Form $form) {
            $form->display('id');
            $form->select('goods_id','产品名称')->options(Goods::pluck('goods_name','id')->toArray());
            $form->datetime('delivery_date');
            $form->select('customer_id','客户名称')->options(Customer::pluck('customer_name','id')->toArray());
            $form->number('num');
            $form->currency('price')->symbol('CNY');
            $form->currency('total_price')->symbol('CNY')->readonly();
            $form->footer(function ($footer) {
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
                $footer->disableCreatingCheck();
            });

            $form->html(
                <<<HTML
                <script>
                // 监听输入框1的输入事件
                $('input[name="price"]').on('input', function () {
                    var price = $(this).val();
                    var num = $('input[name="num"]').val();
                    var total_price = price * num;
                    $('input[name="total_price"]').val(total_price);
                });
                $('input[name="num"]').on('input', function () {
                    var num = $(this).val();
                    var price = $('input[name="price"]').val();
                    var total_price = price * num;
                    $('input[name="total_price"]').val(total_price);
                });
                </script>
HTML
            );

        });
    }
}
